package com.atguigu.auth.controller;


import com.atguigu.auth.entity.Family;
import com.atguigu.auth.entity.Room;
import com.atguigu.auth.service.RoomService;
import com.atguigu.common.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 房间信息表 前端控制器
 * </p>
 *
 * @author universe
 * @since 2024-03-17
 */
@Api(tags = "房间管理接口")
@RestController
@RequestMapping("/auth/room")
public class RoomController {
    @Autowired
    private RoomService service;

    //条件分页查询
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        Room room) {
        //创建page对象
        Page<Room> pageParam = new Page<>(page,limit);

        //封装条件，判断条件值不为空
        LambdaQueryWrapper<Room> wrapper = new LambdaQueryWrapper<>();
        //获取条件值
        String roomName = room.getRoomName();
        String roomType = room.getRoomType();
        //判断条件值不为空
        //like 模糊查询
        if(!StringUtils.isEmpty(roomName)) {
            wrapper.like(Room::getRoomName,roomName);
        }
        if(!StringUtils.isEmpty(roomType)) {
            wrapper.like(Room::getRoomType,roomType);
        }
        //调用mp的方法实现条件分页查询
        IPage<Room> pageModel = service.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "获取")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        Room room = service.getById(id);
        return Result.ok(room);
    }

    @ApiOperation(value = "获取家庭信息列表")
    @GetMapping("getRoomList")
    public Result<List<Room>> getRoomList() {
        List<Room> room = service.list();
        return Result.ok(room);
    }


    @ApiOperation(value = "根据家庭名称获取房间名称")
    @GetMapping("getAllByFamilyId/{id}")
    public Result getAllByFamilyId(@PathVariable Long id) {
        return Result.ok(service.list(new LambdaQueryWrapper<Room>().eq(Room::getFamilyId, id)));
    }

    @ApiOperation(value = "保存")
    @PostMapping("save")
    public Result save(@RequestBody Room room) {
        service.save(room);
        return Result.ok();
    }

    @ApiOperation(value = "更新")
    @PutMapping("update")
    public Result updateById(@RequestBody Room room) {
        service.updateById(room);
        return Result.ok();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        service.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value =  "批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Integer> idList){
        service.removeByIds(idList);
        return Result.ok();
    }
}

